home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C11 / HowMany.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  872 b   |  42 lines

  1. //: C11:HowMany.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Class counts its objects
  7. #include <fstream>
  8. using namespace std;
  9. ofstream out("HowMany.out");
  10.  
  11. class HowMany {
  12.   static int object_count;
  13. public:
  14.   HowMany() {
  15.     object_count++;
  16.   }
  17.   static void print(const char* msg = 0) {
  18.     if(msg) out << msg << ": ";
  19.     out << "object_count = "
  20.          << object_count << endl;
  21.   }
  22.   ~HowMany() {
  23.     object_count--;
  24.     print("~HowMany()");
  25.   }
  26. };
  27.  
  28. int HowMany::object_count = 0;
  29.  
  30. // Pass and return BY VALUE:
  31. HowMany f(HowMany x) {
  32.   x.print("x argument inside f()");
  33.   return x;
  34. }
  35.  
  36. int main() {
  37.   HowMany h;
  38.   HowMany::print("after construction of h");
  39.   HowMany h2 = f(h);
  40.   HowMany::print("after call to f()");
  41. } ///:~
  42.